const db = uniCloud.database();
const xl_goods = db.collection('xl_goods');
const xl_fav = db.collection('xl_fav');
const xl_cart = db.collection('xl_cart');
const xl_cate = db.collection('xl_cate');
const xl_comment = db.collection('xl_comment');
const dbCmd = db.command;
const goods  = {
	
	//根据分类ID获取商品列表
	async get_goods_list(param) {
	
		let {
			cate_id,
			is_top,
			skip,
			limit,
			sort_type
		} = param;
		let where = {}
		if (is_top) {
			let s_cate_id = await xl_cate.where({
				parent_id: cate_id
			}).get();
	
			let arr = s_cate_id.data.map(v => {
				return v._id
			})
			where.cate_id = dbCmd.in(arr)
		} else {
			where.cate_id = cate_id;
		}
		let orderBy = [
			["sales", "desc"],
			["sales", "desc"],
			["price", "asc"],
			['price', 'desc']
		]
		orderBy = orderBy[sort_type - 1];
		let count = await xl_goods.where(where).count();
		let res = await xl_goods.where(where)
			.field({
				"img": true,
				"name": 1,
				"sales": 1,
				"price": 1
			})
			.orderBy(orderBy[0], orderBy[1])
			.skip(skip - 0)
			.limit(limit - 0)
			.get();
		if (res.data) {
			return {
				code: 1,
				data: res.data,
				count: count.total
			}
		} else {
			return {
				code: 0,
				data: res
			}
		}
	},
	
	async get_goods_detail(param) {
	
		let {
			uid,
			goods_id
		} = param;
	
		let is_fav = 0;
	
		if (uid) {
			let fav_data = await xl_fav.where({
				uid,
				goods_id
			}).get();
	
			is_fav = fav_data.data.length;
		}
	
	
		let res = await xl_goods.doc(goods_id).get();
		return {
			code: 1,
			data: res.data,
			is_fav
		}
	},
	async get_goods_comment(param){
		
		let {goods_id,skip=0,limit=10} = param;
		
		let  count = await xl_comment.where({
			goods_id
		}).count();
		
		count = count.total;
		
		if(count==0){
			
			return {
				code:1,
				data:[]
				
			}
			
		}
		
		let res = await xl_comment.where({
			goods_id
		}).skip(skip-0).limit(limit-0).get();
		
		return {
			code:1,
			data:res.data,
			count:count
		}
		
		
	},
	// 精选商品
	async get_competitive(param) {
	
		let {
			skip = 0, limit = 10
		} = param;
		let count = await xl_goods.where({
			"competitive": "1"
		}).count();
		let res = await xl_goods.where({
				"competitive": "1"
			})
			.skip(skip - 0).limit(limit - 0).get();
		return {
			code: 1,
			data: res.data,
			count: count.total
		}
	},
	async get_like(param) {
	
		let {
			skip = 0, limit = 10
		} = param;
		let count = await xl_goods.where({
			"like": "1"
		}).count();
		let res = await xl_goods.where({
				"like": "1"
			})
			.skip(skip - 0).limit(limit)
			.get();
		return {
			code: 1,
			data: res.data,
			count: count.total
		}
	},
	async get_search_list(param) {
	
		let {
			skip = 0, limit = 10,keyword
		} = param;
		let count = await xl_goods.where({
			"name": new RegExp(keyword)
		}).count();
		let res = await xl_goods.where({
				"name": new RegExp(keyword)
			})
			.skip(skip - 0).limit(limit - 10)
			.get();
		return {
			code: 1,
			data: res.data,
			count: count.total
		}
	},
	//收藏
	
	async toggle_fav(param) {
	
		let {
			uid,
			is_fav,
			name,
			price,
			img,
			goods_id
		} = param;
	
		if (!uid) {
			return {
				code: 0,
				msg: "请先登录"
			}
		}
		let res = null;
	
		if (is_fav == 0) {
			//如果没收藏  那就是需要收藏了
			res = await xl_fav.add({
				uid,
				name,
				price,
				img,
				goods_id
			});
			if (res.id) {
				return {
					code: 1,
					is_fav: 1,
					msg: "收藏成功"
				}
			} else {
				return {
					code: 0,
					is_fav: 0,
					msg: "收藏失败"
				}
	
			}
	
	
	
		} else {
			//否则 取消收藏
			res = await xl_fav.where({
				uid,
				goods_id
			}).remove()
			if (res.deleted) {
				return {
					code: 1,
					is_fav: 0,
					msg: "取消成功"
				}
			} else {
				return {
					code: 0,
					is_fav: 1,
					msg: "取消失败"
				}
	
			}
	
		}
	
	
	},
	async get_fav_list(param) {
		let {
			skip = 0, limit = 10, uid
		} = param;
		if (!uid) {
			return {
				code: 0,
				msg: "请先登录"
			}
		}
		let count = await xl_fav.where({
			uid
		}).count();
		let res = await xl_fav.where({
				uid
			})
			.skip(skip - 0).limit(limit - 0).get();
		return {
			code: 1,
			data: res.data,
			count: count.total
		}
	
	},
	//加入购物车
	
	async add_cart(param) {
	
		let {
			uid,
			name,
			price,
			img,
			goods_id,
			num,
			attr,
			type
		} = param;
	
		if (!uid) {
			return {
				code: 0,
				msg: "请先登录"
			}
		}
	
		let check_stock = await xl_goods.where({
			_id: goods_id
		}).field({
			'table': true,
			'stock': true
		}).get();
	
		let real_stock = 0;
		let table = check_stock.data[0].table;
	
	//查库存
		if (table.length == 0) {
	
			real_stock = check_stock.data[0].stock;
	
		} else {
	
			for (let i = 0; i < table.length; i++) {
				if (table[i].text.join() == attr) {
					real_stock = table[i].stock;
					break;
				}
			}
		}
	
		if (num - real_stock > 0) {
	
			return {
				code: 0,
				msg: "库存不足"
			}
		}
	
	let has_old = await xl_cart.where({
		uid,
		goods_id,
		attr
	}).get();
	
	let res = null;
	
	if(has_old.data.length>0&&type!='buy'){
		res = await xl_cart.where({
			uid,
			goods_id,
			attr
		}).update({
		  num: dbCmd.inc(num-0)
		})
	}else{
		
	 res = await xl_cart.add({
			uid,
			name,
			price,
			img,
			goods_id,
			num,
			attr,
			stock: real_stock-0,
			status:1
		});
		
	}
		if (res.id||res.updated) {
			return {
				code: 1,
				msg: "操作成功",
				data:res
			}
		} else {
			return {
				code: 0,
				msg: "操作失败"
			}
	
		}
	
	},
	
	async get_cart_list(param) {
	
		let {
			uid
		} = param;
	
		let res = await xl_cart.where({
			uid,
			status:1
		}).get();
	
	
		return {
			code: 1,
			data: res.data
		}
	
	},
	async remove_cart(param){
		
		let {_id} = param;
	let res = await xl_cart.doc(_id).update({
		status:2
	});
		
		if(res.deleted){
			return {
				code:1,
				msg:"删除成功"
			}
		}else{
			return{
				code:1,
				msg:"删除失败"
			}
		}
	},
	async change_cart_num(param){
		
		let {_id,num} = param;
		let res = await xl_cart.doc(_id).update({
		  num: num
		});
		
		return {
			code:1,
			msg:'修改成功'
		}
		
	}
	
	
}

module.exports =  goods;